package com.yzcrm.erp.psi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Record;
import com.yzcrm.common.annotation.NotNullValidate;
import com.yzcrm.common.annotation.Permissions;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.crm.common.CrmEnum;
import com.yzcrm.erp.oa.common.OaEnum;
import com.yzcrm.erp.oa.service.OaCommentService;
import com.yzcrm.erp.psi.entity.PsiStorehouse;
import com.yzcrm.erp.psi.service.PsiStoreHouseService;
import com.yzcrm.erp.work.entity.TaskComment;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import com.yzcrm.utils.AuthUtil;
import com.yzcrm.utils.R;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiang.xue
 * 仓库管理
 */
@Api(remark = "进销存：仓库管理")
public class PsiStoreHouseController extends Controller{

    @Inject
    private PsiStoreHouseService storehouseService;

    /**
     * @author wyq
     * 查看列表页
     */
    @Permissions({"psi:storehouse:index"})
    @ActionApi(summary = "检索仓库列表数据", remark="检索仓库列表数据")
    @Params({
            @Param(name="sceneId", remark="场景Id", dataType="int", defaultValue="", required=false),
            @Param(name="type", remark="权限标识 61：仓库（storehouse）", dataType="int", defaultValue="", required=true)
    })
    public void queryPageList(BasePageRequest basePageRequest) {

        JSONObject jsonObject = basePageRequest.getJsonObject().fluentPut("type", CrmEnum.PSI_STOREHOUSE.getType());
        basePageRequest.setJsonObject(jsonObject);
        renderJson(storehouseService.filterConditionAndGetPageList(basePageRequest));
    }

    /**
     * 添加或者修改仓库
     */
    @ActionApi(summary = "添加或者修改仓库", remark="添加或者修改仓库")
    @Params({
            @Param(name="address", remark="地址，（辽宁省,大连市,高新园区）", dataType="String", defaultValue="", required=false),
            @Param(name="address_detail", remark="地址详细", dataType="String", defaultValue="", required=false),
            @Param(name="location", remark="定位", dataType="String", defaultValue="", required=false)
    })
    @Permissions({"psi:storehouse:save", "psi:storehouse:update"})
    public void saveOrUpdate(){

        JSONObject jsonObject = JSON.parseObject(getRawData());
        renderJson(storehouseService.addOrUpdate(jsonObject));
    }

    /**
     * @author wyq
     * 根据id删除仓库
     */
    @ActionApi(summary = "根据id删除仓库", remark="根据id删除仓库")
    @Params({
            @Param(name="storehouseIds", remark="仓库ID 多个ID,逗号分割", dataType="String", defaultValue="", required=true)
    })
    @Permissions("psi:storehouse:delete")
    @NotNullValidate(value = "storehouseIds", message = "仓库id不能为空")
    public void deleteByIds(@Para("storehouseIds") String storehouseIds) {
        renderJson(storehouseService.deleteByIds(storehouseIds));
    }

    /**
     * @author wyq
     * 根据仓库id查询
     */
    @ActionApi(summary = "根据id查询仓库", remark="根据id查询仓库")
    @Params({
            @Param(name="storehouseId", remark="仓库ID", dataType="String", defaultValue="", required=true)
    })
    @Permissions("psi:storehouse:read")
    @NotNullValidate(value = "storehouseId", message = "仓库id不能为空")
    public void queryById(@Para("storehouseId") Integer storehouseId) {
        renderJson(R.ok().put("data", storehouseService.queryById(storehouseId)));
    }

    /**
     * @author jiang.xue
     * 查询在库(销售)
     */
    @ActionApi(summary = "查询在库", remark="查询在库")
    @Params({
            @Param(name="productId", remark="产品ID", dataType="Int", defaultValue="", required=false),
            @Param(name="productName", remark="产品名称", dataType="String", defaultValue="", required=false),
            @Param(name="productStatus", remark="1 上架 0 下架 3 删除", dataType="Int", defaultValue="", required=false),
            @Param(name="productNameEn", remark="产品英文名称", dataType="String", defaultValue="", required=false),
            @Param(name="productNum", remark="产品编号", dataType="String", defaultValue="", required=false),
            @Param(name="productSpecName", remark="产品规格", dataType="String", defaultValue="", required=false),
            @Param(name="storehouseId", remark="仓库ID", dataType="Int", defaultValue="", required=false),
//            @Param(name="storehouseCode", remark="仓库编号", dataType="String", defaultValue="", required=false),
            @Param(name="storehouseName", remark="仓库名称", dataType="String", defaultValue="", required=false),
            @Param(name="page", remark="页码", dataType="Int", defaultValue="", required=true),
            @Param(name="limit", remark="每页数量", dataType="Int", defaultValue="", required=true)
//            @Param(name="productBatchId", remark="产品批次号", dataType="String", defaultValue="", required=false),
//            @Param(name="productUpcCode", remark="产品序列号", dataType="String", defaultValue="", required=false),
//            @Param(name="elsNum", remark="出入库编号", dataType="String", defaultValue="", required=false)
    })
    @Permissions({"psi:querystorehouse:querystorehouse", "psi:querystorehouse:querystorehouse_salesman","psi:querystorehouse:querystorehouse_director","psi:querystorehouse:querystorehouse_purchaser"})
    public void queryStorehouse(@Para("productId") Integer productId,
                          @Para("productName") String productName,
                          @Para("productStatus") Integer productStatus,
                          @Para("productNameEn") String productNameEn,
                          @Para("productNum") String productNum,
                          @Para("productSpecName") String productSpecName,
                          @Para("storehouseId") Integer storehouseId,
                          @Para("storehouseCode") String storehouseCode,
                          @Para("storehouseName") String storehouseName,
                          @Para("productBatchId") String productBatchId,
                          @Para("productUpcCode") String productUpcCode,
                          @Para("elsNum") String elsNum,
                               @Para("page") int page,
                               @Para("limit") int limit) {
        renderJson(storehouseService.queryStorehouse(productId,productName,productStatus,productNameEn,productNum,
                productSpecName,storehouseId,storehouseCode,storehouseName,productBatchId,productUpcCode, elsNum, page,limit));
    }

    /**
     * 检索在库表头信息
     */
    @ActionApi(summary = "查询在库 表头", remark="查询在库 表头")
    public void queryStorehouseListHead() {

        renderJson(R.ok().put("data", storehouseService.queryStorehouseListHead()));
    }

}
